import {
	CHANGE_INPUT_VALUE,
	ADD_TODO_ITEM,
	DELETE_TODO_ITEM,
	INIT_LIST
} from './ActionTypes';

const defaultState = {
	inputValue: '',
	list: []
};

const reducer = (state = defaultState, action) => {
	// console.log(state, action);
	const newState = JSON.parse(JSON.stringify(state));
	if (action.type === CHANGE_INPUT_VALUE) {
		newState.inputValue = action.value;
		return newState;
	}
	if (action.type === ADD_TODO_ITEM) {
		if (newState.inputValue !== '') {
			newState.list.push(newState.inputValue);
			newState.inputValue = '';
			return newState;
		}
	}

	if (action.type === DELETE_TODO_ITEM) {
		newState.list.splice(action.index, 1);
		return newState;
	}

	if (action.type === INIT_LIST) {
		newState.list = [...action.data];
		return newState;
	}
	return state;
}

export default reducer;